package cn.example.vip.mall.canal.listener;

import cn.example.vip.mall.page.feign.SeckillPageFeign;
import cn.example.vip.mall.search.entity.SeckillGoodsEs;
import cn.example.vip.mall.search.feign.SeckillGoodsSearchFeign;
import cn.example.vip.mall.seckill.entity.SeckillGoods;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;


@Component
@CanalTable("seckill_goods")
public class SeckillGoodsHandler implements EntryHandler<SeckillGoods> {
	@Autowired
	private SeckillGoodsSearchFeign seckillGoodsSearchFeign;
	@Autowired
	private SeckillPageFeign seckillPageFeign;
	
	@Override
	public void insert(SeckillGoods seckillGoods) {
		// 索引导入
		seckillGoodsSearchFeign.add(JSON.parseObject(JSON.toJSONString(seckillGoods), SeckillGoodsEs.class));
		// 生成/更新静态页
		try {
			seckillPageFeign.html(seckillGoods.getId());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void update(SeckillGoods before, SeckillGoods after) {
		// 索引导入
		seckillGoodsSearchFeign.add(JSON.parseObject(JSON.toJSONString(after), SeckillGoodsEs.class));
		// 生成/更新静态页
		try {
			seckillPageFeign.delHtml(after.getId());
			seckillPageFeign.html(after.getId());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void delete(SeckillGoods seckillGoods) {
		EntryHandler.super.delete(seckillGoods);
	}
}
